package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.domain.store.Question;
import com.itheima.mapper.store.QuestionMapper;
import com.itheima.service.QuestionService;
import com.itheima.utils.MapperUtil;
import com.itheima.utils.UidUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;
import java.util.UUID;

public class QuestionServiceImpl implements QuestionService {
    //查找所有
    @Override
    public List<Question> findAll(String pageNum, String pageSize) {
        //获取Sqlsession对象
        SqlSession session = MapperUtil.getSession();
        //获取mapper对象
        QuestionMapper mapper = session.getMapper(QuestionMapper.class);
        //默认当前页为1
        int pageN=1;
        //默认每页显示条目数为10
        int pageS=10;
        //对获取的当前页码和每页显示条目数进来类型处理
        if(StringUtils.isNotBlank(pageNum)){
            pageN=Integer.parseInt(pageNum);
        }
        if(StringUtils.isNotBlank(pageSize)){
            pageS=Integer.parseInt(pageSize);
        }
        //使用分页插件设置分页参数
        PageHelper.startPage(pageN,pageS);

        //调用方法
        List<Question> list = mapper.findAll();
        //关闭资源
        MapperUtil.close(session);
        return list;
    }

    //新建
    @Override
    public String save(Question question) {
        //获取Sqlsession对象
        SqlSession session = MapperUtil.getSession();
        //获取mapper对象
        QuestionMapper mapper = session.getMapper(QuestionMapper.class);

        //主键id处理
       // question.setId(UidUtil.getUid());
        String id = UUID.randomUUID().toString();
        question.setId(id);

        //设置新创建题目的审核状态为未审核（0）
        question.setReviewStatus("0");
        question.setCreateTime(new Date());
        //设置当前存储的图片名称为id值
        question.setPicture(id);

        mapper.save(question);
        //3、关闭资源
        MapperUtil.close(session);

        return id;
    }

    //根据id删除
    @Override
    public Boolean deleteByIds(String ids) {
        SqlSession session=null;
        boolean flag=false;//删除失败
        try {
            //1、获取sesssion
            session = MapperUtil.getSession();
            //2、获取代理对象
            QuestionMapper mapper = session.getMapper(QuestionMapper.class);
            String[] id = ids.split(",");//["1","2","3"]
            mapper.deleteByIds(id);
            flag=true;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //3、关闭资源
            MapperUtil.close(session);
        }

        return flag;
    }

    //根据id查找
    @Override
    public Question findById(String id) {
        //1、获取sesssion
        SqlSession session = MapperUtil.getSession();
        //2、获取代理对象
        QuestionMapper mapper = session.getMapper(QuestionMapper.class);
        Question question=mapper.findById(id);
        //3、关闭资源
        MapperUtil.close(session);
        return question;
    }

    //更新数据
    @Override
    public void update(Question question,Boolean flag) {
        //1、获取sesssion
        SqlSession session = MapperUtil.getSession();
        //2、获取代理对象
        QuestionMapper mapper = session.getMapper(QuestionMapper.class);

        if(flag) {
            //设置当前存储的图片名称为id值
            question.setPicture(question.getId());
        }

        mapper.update(question);

        //3、关闭资源
        MapperUtil.close(session);
    }
}
